#ifndef __RYN_SOCKET_H_
#define __RYN_SOCKET_H_

#ifdef __cplusplus
extern "C" {
#endif

#ifndef UART_BUF_SIZE
#define UART_BUF_SIZE 4096
#define UART_BUF_SIZE2 5200
#endif

#define PrintByteArrayDebug 1
#define PrintByteArray(ti,data,len) \
	do{							  \
	if(PrintByteArrayDebug){      \
	int i = 0;					  \
	printf("%s ：",ti);			  \
	for(i = 0;i<len;i++){		  \
	printf("%02X ",data[i]);	  \
	}							  \
	printf("\n");				  \
	}							  \
	}while(0)
#ifdef  WIN32
#ifndef SHUT_RDWR
#define SHUT_RDWR SD_BOTH
#endif
#ifndef SHUT_WR
#define SHUT_WR SD_SEND //SD_SEND
#endif
#ifndef SHUT_RD
#define SHUT_RD SD_RECEIVE
#endif
	#include <WinSock2.h>
#include <minwinbase.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#endif //  WIN32


#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
        
#ifndef SOCKET
#define SOCKET int
#endif

#define fn_WriteTimeout write_timeout
#define fn_NewSocket newsocket
#define fn_WritePkg writepkg
#define fn_ReadTimeout read_timeout
#define fn_ReadPkg readpkg


	typedef struct SocketServerST
	{
		SOCKET serverSocketFD;
		void* (*client_thread_funciton)(void * arg);
		int (*onSocketClose)(int socket);
		int *running;
		char bindHost[64];
		int bindPort;
		struct sockaddr_in *clientAddr;
		SOCKET clientSocket;
	}SocketServerST_t;
	typedef struct SocketClientST{
		uint8_t *frame_buf;
		int frame_len;
		int sockfd;
	} SocketClientST_t;

int initsock(void );
int newsocket(unsigned char *pHost,unsigned int hostLen,unsigned int port);
int writepkg(int sockfd,char *pSendBuf,int bufLen);
int readpkg(int sockfd,char *pRecvBuf,int bufLen);
void ShutDownRYnSocket(int sockfd,int how);
int CloseRYnSocket(int sockfd);
void set_keepalive(int sockfd);
int read_timeout(int fd, unsigned int wait_seconds);
int write_timeout(int fd, unsigned int wait_seconds);
int accept_timeout(int fd, struct sockaddr_in *addr, unsigned int wait_seconds);
void activate_nonblock(int fd);
void deactivate_nonblock(int fd);
int connect_timeout(int fd, struct sockaddr_in *addr, unsigned int wait_seconds);


int SendDataGramPkg(int sockfd,char *buf,int bufLen,struct sockaddr_in server);
int ReadDataGramPkg(int sockfd,char *buf,int nMaxBufLen,struct sockaddr_in server);
int NewDataGramSocket(struct sockaddr_in * server,unsigned char *pHost,unsigned int hostLen,unsigned int port);
int NewDataGramServer(struct sockaddr_in *local, int server_port);
int ReadDataGramPkg0(int sockfd,char *buf,int nMaxBufLen,struct sockaddr_in *server);
int SendDataGramPkg0(int sockfd,char *buf,int bufLen,struct sockaddr_in *server);

int NewSocketServer(char *bindHost,int bindPort,int *running,void* protocol_accept_thread_function(void *arg),void* (*client_thread_funciton)(void * arg),int (*onSocketClose)(int socket));
int NewFeiLuSocketServer(char *bindHost,int bindPort,int *running,void* (*client_thread_funciton)(void * arg),int (*onSocketClose)(int socket));
int WriteFeiLuProtocolPkg(int sockfd,char *data, int data_len);

int IsBigEndian();
// 16位 大小端转换
int ucs2_swap_endian(char *in, int ilen);
// ucs2转utf8
int ucs2_to_utf8(unsigned char *in, int ilen, unsigned char *out, int olen);
// utf8转ucs2
int utf8_to_ucs2( unsigned char *in, int ilen, unsigned char *out, int olen);
    
#ifdef __cplusplus
	}
#endif

#endif
